<template>
  <div class='container'>
    <el-card class="box-card">
      <div class="text item">
        <!-- 关键字 -->
        <el-form :inline="true" :model="formWord" ref="formWord" class="demo-form-inline">
          <el-form-item label="关键字" prop="keyword">
            <el-input v-model="formWord.keyword" placeholder="根据文章标题搜索"></el-input>
          </el-form-item>
          <!-- 状态 -->
          <el-form-item label="状态" prop='state'>
            <el-select v-model="formWord.state" placeholder="请选择">
              <el-option label="启用" value="0"></el-option>
              <el-option label="禁用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <!-- 清除搜索 -->
          <el-form-item>
            <el-button @click="hDelete" size="small">清除</el-button>
            <el-button type="primary" @click="hQuery" size="small">搜索</el-button>
          </el-form-item>
          <!-- 新增文章 -->
          <el-form-item class="filter-item fr">
            <el-button
              type="success"
              size="small"
              @click="addComponey"
              style="margin-left:6px"
              icon="el-icon-edit">
              新增技巧</el-button>
          </el-form-item>
      </el-form>
      </div>
      <!-- 中间阴影 -->
      <el-alert
        v-if="alertText !== ''"
        :title="alertText"
        type="info"
        class="alert"
        :closable="false"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :key="tableKey"
        :data='tableData'
        v-loading='isLoading'
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%">
      <!-- 序号 -->
      <el-table-column prop="id"  label="序号" width="100" type="index">
      </el-table-column>
      <!-- 文章标题 -->
      <el-table-column label="文章标题" width="400">
        <template slot-scope="scope">
          <span>{{scope.row.title}}</span>
          <el-button type="text" :class="{'el-icon-film': scope.row.videoURL}" @click="clickVideo(scope.row.videoURL)">
          </el-button>
        </template>
      </el-table-column>
      <!-- 阅读数 -->
      <el-table-column  label="阅读数" width="150">
        <template slot-scope="scope">
          <span>{{scope.row.visits}}</span>
        </template>
      </el-table-column>
      <!-- 录入人 -->
      <el-table-column label="录入人" width="150">
        <template slot-scope="scope">
          <span>{{scope.row.username}}</span>
        </template>
      </el-table-column>
      <!-- 录入时间 -->
      <el-table-column width="170" prop="createTime"  label="录入时间" :formatter="dataFormat">
      </el-table-column>
      <!-- 状态 -->
      <el-table-column  label="状态" width="80">
        <template slot-scope="scope">
          <span >{{scope.row.state ===0 ? '已启用' : '已禁用'}}</span>
        </template>
      </el-table-column>
        <!-- 操作 -->
      <el-table-column
      label="操作" width="280">
      <template slot-scope="scope">
        <el-button size="medium " type="text" @click="hPreview(scope.row.id)">预览</el-button>
        <el-button size="medium " type="text" @click="hStatus(scope.row)" v-if="scope.row.state==1">启用</el-button>
        <el-button size="medium " type="text" @click="hStatus(scope.row)" v-else>禁用</el-button>
        <el-button size="medium " type="text" @click="hModify(scope.row.id)" :disabled='scope.row.state===0'>修改</el-button>
        <el-button size="medium " type="text" @click="delRemove(scope.row.id)" :disabled='scope.row.state===0' >删除</el-button>
      </template>
      </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        style="float: right; margin: 10px;"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listParams.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="listParams.pagesize"
        layout="  prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 新增弹窗   修改弹窗 -->
    <el-dialog :title="addTitle"
      width='52%'
      :visible.sync="isShowDialog"
      v-if="isShowDialog"
      >
      <ArticlesAdd ref="editUser"
      :companyId="titleId"
      @close="isShowDialog=false"
      v-model="titleId"
      @refresh="getList"></ArticlesAdd>
    </el-dialog>
    <!-- 预览弹窗 -->
    <el-dialog :title="previewTitle"
      width='52%'
      :visible.sync="isPrviewDialog"
      v-if="isPrviewDialog"
      >
    <ArticlesPriview ref="prviewUser"
      @close="isPrviewDialog=false"
      v-model="userId"
      @refresh="getList"></ArticlesPriview>
    </el-dialog>
    <!-- 视频弹窗 -->
    <el-dialog
      center
      width='800' height='500'
      :before-close="closeVideo"
      :visible.sync="isVideo">
      <el-button @click="close" class="close" icon="el-icon-close" circle></el-button>
      <video class="video" width="100%" height="100%" loop controls autoplay :src="videos"></video>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import ArticlesAdd from './../components/articles-add'
import ArticlesPriview from './../components/articles-preview'
export default {
  components: {
    ArticlesAdd,
    ArticlesPriview
  },
  data () {
    return {
      formWord: {
        keyword: null, // 关键字
        state: null // 状态
      },
      tableData: [],
      listParams: {
        page: 1, // 页码
        pagesize: 10 // 每页显示条数
      },
      curCompanyId: null, // 编辑修改
      alertText: '',
      total: 0, // 总条数
      tableKey: 0,
      isLoading: false, // 是否显示
      isShowDialog: false, // 新增修改
      isPrviewDialog: false, // 预览
      isVideo: false, // 视频显示
      videos: '', // 视频
      id: null,
      userId: '', // 预览页面的id
      titleId: '' // 头部信息
    }
  },
  // 状态
  computed: {
    addTitle () {
      return this.titleId ? '修改文章' : '新增文章'
    },
    previewTitle () {
      return '预览文章'
    }
  },
  created () {
    this.intialeData()
  },
  methods: {
    // 初始数据
    intialeData () {
      this.getList()
    },
    setList () {
      this.listParams.keyword = this.formWord.keyword
      this.listParams.state = this.formWord.state
      return this.listParams
    },
    // 获取列表信息
    async getList () {
      const param = this.setList()
      this.isLoading = true
      try {
        const { data: res } = await list(param)
        console.log(res)
        console.log(res.items[0])
        // 获取的数据给datalist
        this.tableData = res.items
        // 获取的总条数
        this.total = res.counts
        this.alertText = `共 ${this.total} 条记录`
        this.isLoading = false
      } catch (err) {
        console.log(err)
        this.isLoading = true
      }
    },
    // 清空表单
    hDelete () {
      // el 表单组件上有一个resetFields方法
      this.$refs.formWord.resetFields()
      this.listParams.page = 1
      this.getList()
    },
    // 进入下一页
    handleCurrentChange (curPage) {
      this.listParams.page = curPage
      this.getList()
    },
    // 每页显示的信息条数
    handleSizeChange (val) {
      this.listParams.pagesize = val
      this.listParams.page = 1
      this.getList()
    },
    // 删除
    async doDelete (id) {
      try {
        await remove({ id })
        this.$message.success('删除成功!')
        this.tableData.splice('user', 1)
        // 刷新页面
        this.getList(this.listParams)
      } catch (err) {
        this.$message.error('删除失败')
      }
    },
    delRemove (user) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDelete(user)
      }).catch(() => {
        this.$message.info('已取消操作!')
      })
    },
    // 搜索信息
    hQuery () {
      // 重置页码
      this.listParams.page = 1
      // 重发请求
      this.getList()
      console.log(this.listParams)
    },
    // 新增用户、编辑用户
    addComponey () {
      this.titleId = ''
      this.isShowDialog = true
    },
    // 编辑
    hModify (id) {
      this.titleId = id
      // this.curCompanyId = id
      this.isShowDialog = true
    },
    // 预览
    hPreview (id) {
      this.userId = id
      this.isPrviewDialog = true
    },
    // 启用  禁用
    async hStatus (row) {
      const switchState = row.state === 1 ? 0 : 1
      const data = {
        id: row.id,
        state: switchState
      }
      try {
        const { data: res } = await changeState(data)
        console.log(res)
        this.$message.success(row.state === 1 ? '禁用成功' : '启用成功')
        // this.listParams()
        row.state = (row.state === 0 ? 1 : 0)
      } catch (err) {
        console.log(err)
        this.$message.error('操作失败')
      }
    },
    // 视频播放
    clickVideo (videos) {
      this.isVideo = true
      this.videos = videos
    },
    // 视频停止  清空视频
    closeVideo () {
      this.videos = ''
      this.isVideo = false
    },
    // 关闭视频按钮
    close () {
      this.videos = ''
      this.isVideo = false
    },
    // 时间格式
    dataFormat (row, column) {
      var date = row[column.property]
      if (date) {
        return moment(date).format('YYYY-MM-DD HH:mm:ss')
      } else {
        return ''
      }
    }
  }
}
</script>
<style scoped lang="scss">
.alert {
  margin: 10px 0px;
}
.container {
  margin: 10px 10px;
}
.video ::deep .el-dialog__header {
  padding: 0;
}
.video ::deep .el-dialog__body {
  padding: 0;
  background-color: rgba(0,0,0,1);
}
.close {
  position: fixed;
  left: 50%;
  top: 25px;
  color: #fff;
  background-color: rgba(255, 255, 255, .2);
}
.el-icon-film {
  color: #00f;
  font-size: 18px;
}
</style>
